 package com.fizz.redpacket.controller;


 import com.fizz.redpacket.entities.UserGetRecord;
/*     */ import com.fizz.redpacket.entities.UserSendRecord;
/*     */ import com.fizz.redpacket.error.NoSuchRedPacket;
/*     */ import com.fizz.redpacket.response.Response;
/*     */ import com.fizz.redpacket.response.Status;
/*     */ import com.fizz.redpacket.service.RedPacketService;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import lombok.extern.slf4j.Slf4j;
 /*     */
 /*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.beans.factory.annotation.Qualifier;
/*     */ import org.springframework.web.bind.annotation.RequestBody;
/*     */ import org.springframework.web.bind.annotation.RequestMapping;
/*     */ import org.springframework.web.bind.annotation.RequestMethod;
/*     */ import org.springframework.web.bind.annotation.RequestParam;
/*     */ import org.springframework.web.bind.annotation.RestController;

 @Slf4j
 @RestController
 public class RedPacketController
 {
   private final String sendRedPacketSuffix = "/send";
   private final String grabRedPacketSuffix = "/grab";
   private final String getSendRedPacketSuffix = "/send_query";
   private final String getRedPacketSuffix = "/get_query";

   @Autowired
   @Qualifier("RedPacketServiceImpl")
   RedPacketService redPacketService;


   @RequestMapping(value = {"/index"}, method = {RequestMethod.POST}, consumes = {"application/json"})
   public String getId(@RequestBody Map<String, Object> ids) {
     System.out.println(ids);
     return "di";
   }

   @RequestMapping(value = {"/send"}, method = {RequestMethod.POST}, consumes = {"application/json"})
   public Response sendPacket(@RequestBody Map<String, Object> params) {
     String redPacketId;
     System.out.println(params);
     String userId = (String)params.get("id");
     String amount = (String)params.get("money");
     int redPacketCount = ((Integer)params.get("count")).intValue();

     if (userId == null || amount == null || redPacketCount <= 0) {
       return new Response(Status.PARAMETER_ERROR);
     }

     try {
       Double double_ = Double.valueOf(Double.parseDouble(amount));
     } catch (Exception exception) {
       return new Response(Status.PARAMETER_ERROR, exception.getMessage());
     }


     try {
       redPacketId = this.redPacketService.sendPacket(userId, amount, redPacketCount);
     } catch (Exception e) {
       return new Response(Status.FAILED);
     }

     Map<String, Object> data = new HashMap<>();
     data.put("rePacketId", redPacketId);
     return new Response(Status.SUCCESS, data);
   }


   @RequestMapping(value = {"/grab"}, method = {RequestMethod.GET})
   public Response grabRedPacket(@RequestParam("packetId") String redPacketId, @RequestParam("userId") String userId) {
     UserGetRecord userGetRecord;
     try {
       userGetRecord = this.redPacketService.grabRedPacket(userId, redPacketId);
     } catch (NoSuchRedPacket ex) {
       return new Response(Status.FAILED, ex.getMessage());
     }

     Map<String, Object> data = new HashMap<>();
     data.put("usergetrecord", userGetRecord);
     return new Response(Status.SUCCESS, data);
   }

   @RequestMapping(value = {"/send_query"}, method = {RequestMethod.GET})
   public Response getSendRed(@RequestParam("userId") String userId, @RequestParam("limit") int limit, @RequestParam("offset") int offset) {
     List<UserSendRecord> userSendRecords = this.redPacketService.getUserSendRecord(userId, limit, offset);
     Map<String, Object> map = new HashMap<>();
     map.put("data", userSendRecords);
     return new Response(Status.SUCCESS, map);
   }

   @RequestMapping(value = {"/get_query"}, method = {RequestMethod.GET})
   public Response getUserGetRed(@RequestParam("userId") String userId, @RequestParam("limit") int limit, @RequestParam("offset") int offset) {
     List<UserGetRecord> userGetRecords = this.redPacketService.getUserGetRecord(userId, limit, offset);
     Map<String, Object> map = new HashMap<>();
     map.put("data", userGetRecords);
     return new Response(Status.SUCCESS, map);
   }
 }

